﻿using System;
using System.Collections.Generic;
using System.Threading;

namespace U01_boke
{
    /// <summary>
    /// 缓存管理
    /// </summary>
    public class CacheManage
    {
        public CacheManage()
        {

        }
        static CacheManage()
        {
            //开启一个线程，清理过期缓存
            new Action(() =>
            {
                while (true)
                {
                    Thread.Sleep(10000);
                    var keys = DictionaryContainer.Keys;
                    foreach (var key in keys)
                    {
                        var item = DictionaryContainer[key];
                        if (DateTime.Now >= item.Value)
                        {
                            DictionaryContainer.Remove(key);
                        }
                    }
                }
            }).BeginInvoke(null, null);
        }
        /// <summary>
        /// 静态字典
        /// </summary>
        private static Dictionary<string, KeyValuePair<object, DateTime>> DictionaryContainer = new Dictionary<string, KeyValuePair<object, DateTime>>();
        /// <summary>
        /// 线程锁
        /// </summary>
        private static readonly object _locker = new object();
        /// <summary>
        /// 添加到缓存
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="data">缓存数据</param>
        /// <param name="cacheTime">过期时间</param>
        public static void Add(string key, object data, int cacheTime = 30)
        {
            if (DictionaryContainer.ContainsKey(key))
                DictionaryContainer.Remove(key);
            //加锁
            lock (_locker)
            {
                DictionaryContainer[key] = new KeyValuePair<object, DateTime>(data, DateTime.Now.AddMinutes(cacheTime));
            }
        }
        /// <summary>
        /// 验证缓存是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool Contain(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                return false;
            }
            return DictionaryContainer.ContainsKey(key);
        }
        /// <summary>
        /// 获取缓存，不存在则返回默认值,过期也返回默认值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T Get<T>(string key)
        {
            if (Contain(key))
            {
                if (DictionaryContainer[key].Value <= DateTime.Now)
                    return default(T);
                return (T)DictionaryContainer[key].Key;
            }
            return default(T);
        }
        /// <summary>
        /// 删除单个缓存
        /// </summary>
        /// <param name="key"></param>
        public static void Remove(string key)
        {
            if (Contain(key))
                DictionaryContainer.Remove(key);
        }
        /// <summary>
        /// 删除所有缓存
        /// </summary>
        public static void RemoveAll()
        {
            DictionaryContainer = new Dictionary<string, KeyValuePair<object, DateTime>>();
        }

    }
}
